
export function isnull(a, b) {
    if (typeof (b) == "undefined") b = '';
    if (typeof (a) == "undefined") a = '';
    if (a == null) a = '';
    a = a + '';
    if (a.toLowerCase() == 'undefined') a = '';
    if (a.toLowerCase() == 'null') a = '';
    a = a.replace(/(^\s*)|(\s*$)/g, "");
    if (a == '') a = b;
    return a
}

export function Format(fmt) { //author: meizz
    if (typeof (fmt) == "undefined" || fmt == '') fmt = 'yyyy-MM-dd hh:mm:ss';
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

export function storage(key, value) {
    if (typeof (localstorage) == "undefined") {
        if (typeof (value) == "undefined") {
            let a = '';
            try {
                a = uni.getStorageSync(key) || ''
            } catch (e) { }
            return a
        } else {
            var v = value;
            try {
                uni.setStorageSync(key, value);
            } catch (e) { }
        }
    } else {
        if (typeof (value) == "undefined") {
            let a = '';
            try {
                v = localstorage.getItem(key)
                if (!v) {
                    return a;
                }
                if (v.indexOf('obj-') === 0) {
                    v = v.slice(4);
                    return JSON.parse(v);
                } else if (v.indexOf('str-') === 0) {
                    return v.slice(4);
                }
            } catch (e) { }
            return a
        } else {
            var v = value;
            try {
                if (typeof v == 'object') {
                    v = JSON.stringify(v);
                    v = 'obj-' + v;
                } else {
                    v = 'str-' + v;
                }
                localstorage.setItem(key, v)
            } catch (e) { }
        }
    }
};
export function comfirm(content, options, yes, cancel) {
    var uni_options = { confirmColor: '#dd524d' }
    switch (typeof (options)) {
        case 'function': cancel = yes; yes = options; options = {}; break;
        case 'object': break;
        default: options = {}; break;
    }
    yes = typeof (yes) === 'function' ? yes : '';
    cancel = typeof (cancel) === 'function' ? cancel : '';

    uni_options.content = content || '提示';
    if (options.title) uni_options.title = options.title;

    const a = options.btn;
    if (typeof a === "string") {
        uni_options.confirmText = a || "确定";
        uni_options.showCancel = false
    }
    if (a instanceof Array) {
        uni_options.confirmText = a[0] || "确定";
        uni_options.cancelText = a[1] || "取消";
    }

    if (yes != '' || cancel != '') {
        uni_options.success = res => {
            if (res.confirm) {
                yes && yes()
            } else if (res.cancel) {
                cancel && cancel()
            }
        }
    }
    uni.showModal(uni_options);
    // uni.showModal({
    // 	confirmColor:'#dd524d',
    // 	content,
    // 	title,
    // 	success: function (res) {
    // 		cbk && cbk(res)
    // 	}
    // });
};
function GetTimeByTimeStr(dateString) {
    var timeArr = dateString.split(" ");
    var d = timeArr[0].split("-");
    var t = timeArr[1].split(":");
    return new Date(d[0], d[1] - 1, d[2], t[0], t[1], t[2]);
}

export function timeago(dateTimeStamp) {   //dateTimeStamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。
    dateTimeStamp = GetTimeByTimeStr(dateTimeStamp)
    // dateTimeStamp = new Date(dateTimeStamp).getTime()
    // alert(dateTimeStamp)
    var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
    var hour = minute * 60;
    var day = hour * 24;
    var week = day * 7;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();   //获取当前时间毫秒
    var diffValue = now - dateTimeStamp;//时间差

    if (diffValue < 0) {
        return;
    }
    var minC = (diffValue / minute) * 1;  //计算时间差的分，时，天，周，月
    var hourC = (diffValue / hour) * 1;
    var dayC = (diffValue / day) * 1;
    var weekC = (diffValue / week) * 1;
    var monthC = (diffValue / month) * 1;
    let result = '';
    if (monthC >= 1 && monthC <= 3) {
        result = " " + monthC.toFixed(0) + "月前"
    } else if (weekC >= 1 && weekC <= 3) {
        result = " " + weekC.toFixed(0) + "周前"
    } else if (dayC >= 1 && dayC <= 6) {
        result = " " + dayC.toFixed(0) + "天前"
    } else if (hourC >= 1 && hourC <= 23) {

        result = " " + hourC.toFixed(0) + "小时前"
    } else if (minC >= 1 && minC <= 59) {
        result = " " + minC.toFixed(0) + "分钟前"
    } else if (diffValue >= 0 && diffValue <= minute) {
        result = "刚刚"
    } else {
        var datetime = new Date();
        datetime.setTime(dateTimeStamp);
        var Nyear = datetime.getFullYear();
        var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
        var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
        var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
        var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
        var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
        result = Nyear + "-" + Nmonth + "-" + Ndate
    }
    return result;
}

export function valid(a, b) {
    if (b) {
        if (b instanceof Array) {
            for (var prop of b) {
                if (isnull(a[prop]) == '') return prop + '为空';
            }
        } else {
            for (var prop in b) {
                var t = b[prop]
                if (isnull(a[prop]) == '') return t.kts ? t.kts : (prop + '为空');
                if (isnull(t.lx) == 'sj') {
                    if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(isnull(a[prop]))) return t.yzts ? t.yzts : '请输入正确的手机号码';
                }
            }
        }
    } else {
        for (var prop in a) {
            if (isnull(a[prop]) == '') return prop + '为空';
        }

    }
    return '';
}

// 时间戳
export function timestamp(tm) {
    // tm=tm?new Date(tm):new Date();
    if (tm) {
        tm = tm.replace(/\-/g, '/')
        tm = new Date(tm)
    } else {
        tm = new Date()
    }
    return tm.getTime();
}

//  断开变量绑定
export function jj(a) {
    try {
        return JSON.parse(JSON.stringify(a));
    } catch (error) {
        return {}
    }
}






export function nav(url, m) {
    if (url == 'back') {
        if (m) uni.navigateBack({ delta: m });
        else uni.navigateBack();
    } else {
        if (m == 'R') uni.reLaunch({ url });
        else if (m) uni.redirectTo({ url });
        else uni.navigateTo({ url });
    }
};



export function msg(msg, icon) {
    let iconlocal = ''
    if (iconlocal == undefined) {
        iconlocal = 'none'
    }
    uni.showToast({
        icon: iconlocal,
        title: msg
    });
};


/* 时间 格式化*/
export function dateTime(time, type) {

    // var date = new Date(time.replace(/-/g, '/'));
    // let gettime = time.getTime()
    let dateIndex = new Date(time)
    let y = dateIndex.getFullYear() | '';
    let m = dateIndex.getMonth() + 1 | '';
    let d = dateIndex.getDate() | '';
    let h = dateIndex.getHours() | '';
    let mm = dateIndex.getMinutes() | '';
    let s = dateIndex.getSeconds() | '';
    let backData
    if (type == 'date') {
        backData = `${y}年${num(m)}月${num(d)}日 ${num(h)}:${num(mm)}:${num(s)}`;
    } else if (type = "nyrssfm") {
        backData = `${y}-${num(m)}-${num(d)} ${num(h)}:${num(mm)}:${num(s)}`;
    } else {
        backData = `${y}-${num(m)}-${num(d)}`;
    }
    return backData;
}
function num(e) {
    return e < 10 ? '0' + e : e
}







